package xunmeng.web.servlet;

import xunmeng.domain.Admin;
import xunmeng.domain.User;
import xunmeng.service.UserService;
import xunmeng.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/loginservlet")
public class loginservlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name=req.getParameter("user");
        String password=req.getParameter("password");
        String checkcode=req.getParameter("verifycode");
//        System.out.println(req.getSession().getAttribute("checkcode"));
        if (checkcode!=null&&checkcode.equalsIgnoreCase((String)req.getSession().getAttribute("checkcode"))){
            Admin admin=new Admin();
            admin.setName(name);
            admin.setPassword(password);
            UserService userService=new UserServiceImpl();
            admin=userService.checkUser(admin);
            if(admin!=null){
//                System.out.println(admin.getAdminId()+";"+admin.getName()+";"+admin.getPassword());
                req.getSession().setAttribute("admin",admin);
                resp.sendRedirect(req.getContextPath()+"/index.jsp");
            }else{
                req.setAttribute("error","用户名或密码错误请重新输入！");
                req.getRequestDispatcher("/login.jsp").forward(req,resp);
            }
        }else {
            req.setAttribute("error","验证码错误请重新输入！");
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }
        req.getSession().removeAttribute("checkcode");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
